// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.skinnedmodel.animation.AnimationPlayer;
import zombie.core.skinnedmodel.model.Model;
import zombie.iso.Vector3;
import zombie.iso.objects.IsoDeadBody;
import zombie.network.GameClient;

public final class ZombieOnGroundState extends State {
    private static final ZombieOnGroundState _instance = new ZombieOnGroundState();
    static Vector3 tempVector = new Vector3();
    static Vector3 tempVectorBonePos = new Vector3();

    public static ZombieOnGroundState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        owner.setCollidable(false);
        if (!owner.isDead()) {
            owner.setOnFloor(true);
        }

        if (owner.isDead() || isoZombie.isFakeDead()) {
            owner.die();
        } else if (!isoZombie.isBecomeCrawler()) {
            if (!"Tutorial".equals(Core.GameMode)) {
                owner.setReanimateTimer((float)(Rand.Next(60) + 30));
            }

            if (GameClient.bClient && isoZombie.isReanimatedPlayer()) {
                IsoDeadBody.removeDeadBody(isoZombie.networkAI.reanimatedBodyID);
            }

            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Idle);
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        if (owner.isDead() || isoZombie.isFakeDead()) {
            owner.die();
        } else if (isoZombie.isBecomeCrawler()) {
            if (!isoZombie.isBeingSteppedOn() && !isoZombie.isUnderVehicle()) {
                isoZombie.setCrawler(true);
                isoZombie.setCanWalk(false);
                isoZombie.setReanimate(true);
                isoZombie.setBecomeCrawler(false);
            }
        } else {
            if (owner.hasAnimationPlayer()) {
                owner.getAnimationPlayer().setTargetToAngle();
            }

            owner.setReanimateTimer(owner.getReanimateTimer() - zombie.GameTime.getInstance().getMultiplier() / 1.6F);
            if (owner.getReanimateTimer() <= 2.0F) {
                if (GameClient.bClient) {
                    if (owner.isBeingSteppedOn() && !isoZombie.isReanimatedPlayer()) {
                        owner.setReanimateTimer((float)(Rand.Next(60) + 30));
                    }
                } else if (owner.isBeingSteppedOn() && isoZombie.getReanimatedPlayer() == null) {
                    owner.setReanimateTimer((float)(Rand.Next(60) + 30));
                }
            }
        }
    }

    public static boolean isCharacterStandingOnOther(IsoGameCharacter chrStanding, IsoGameCharacter chrProne) {
        AnimationPlayer animationPlayer = chrProne.getAnimationPlayer();
        int _int = DoCollisionBoneCheck(chrStanding, chrProne, animationPlayer.getSkinningBoneIndex("Bip01_Spine", -1), 0.32F);
        if (_int == -1) {
            _int = DoCollisionBoneCheck(chrStanding, chrProne, animationPlayer.getSkinningBoneIndex("Bip01_L_Calf", -1), 0.18F);
        }

        if (_int == -1) {
            _int = DoCollisionBoneCheck(chrStanding, chrProne, animationPlayer.getSkinningBoneIndex("Bip01_R_Calf", -1), 0.18F);
        }

        if (_int == -1) {
            _int = DoCollisionBoneCheck(chrStanding, chrProne, animationPlayer.getSkinningBoneIndex("Bip01_Head", -1), 0.28F);
        }

        return _int > -1;
    }

    private static int DoCollisionBoneCheck(IsoGameCharacter isoGameCharacter1, IsoGameCharacter isoGameCharacter0, int int0, float float2) {
        float float0 = 0.3F;
        Model.BoneToWorldCoords(isoGameCharacter0, int0, tempVectorBonePos);

        for (int int1 = 1; int1 <= 10; int1++) {
            float float1 = (float)int1 / 10.0F;
            tempVector.x = isoGameCharacter1.x;
            tempVector.y = isoGameCharacter1.y;
            tempVector.z = isoGameCharacter1.z;
            tempVector.x = tempVector.x + isoGameCharacter1.getForwardDirection().x * float0 * float1;
            tempVector.y = tempVector.y + isoGameCharacter1.getForwardDirection().y * float0 * float1;
            tempVector.x = tempVectorBonePos.x - tempVector.x;
            tempVector.y = tempVectorBonePos.y - tempVector.y;
            tempVector.z = 0.0F;
            boolean _boolean = tempVector.getLength() < float2;
            if (_boolean) {
                return int0;
            }
        }

        return -1;
    }

    @Override
    public void exit(IsoGameCharacter owner) {
    }
}
